package project.controllers.workers;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.List;

import project.controllers.sort.SortOrderByDeadlineDate;
import project.model.DataBase;
import project.model.IGarage;
import project.model.IMaster;
import project.model.IOrder;
import project.model.OrderStatus;

public class DBManager {

	private ArrayList<IOrder> orderList = new ArrayList<IOrder>();
	private ArrayList<IMaster> masterList = new ArrayList<IMaster>();
	private ArrayList<IGarage> garageList = new ArrayList<IGarage>();
	private DataBase db;
	private static final String WRONG_FORMAT="Sorry wrong date's format";
	
	
	public DBManager(String masPath, String ordPath, String garPath){
		db = new DataBase(masPath,ordPath,garPath);
		try {
			masterList.addAll(db.getMasterList());
			garageList.addAll(db.getGarageList());
			orderList.addAll(db.getOrderList());
		
		}catch (NumberFormatException e) {
			System.out.println(WRONG_FORMAT);
		}catch(ParseException e){
			System.out.println(WRONG_FORMAT);
		}
	}
	

	public void getEmptyGarage() {
		WorkerWithGarage.getEmptyGarage(garageList);
	}

	public void getOrdersByMaster(IMaster master) {
		WorkerWithMaster.getOrdersByMaster(master);
	}

	public void getMastersByOrder(IOrder order) {
		WorkerWithOrder.getMastersByOrder(order);
	}

	public void getNumberEmptyPlaces(GregorianCalendar date) {
		WorkerWithGarage.getNumberEmptyPlaces(masterList, garageList, date);
	}

	public void getNextFreeDate() {
		WorkerWithOrder.getNextFreeDate(orderList);
	}

	public void SortOrder(Comparator sortBy) {
		WorkerWithOrder.SortOrder(orderList, sortBy);
	}

	public void SortMaster(Comparator sortBy) {
		WorkerWithMaster.SortMaster(masterList, sortBy);
	}

	public void SortMadingOrder(Comparator sortBy) {
		WorkerWithOrder.getListMadingOrder(orderList, sortBy);

	}

	public void SortOrderByPeriod(OrderStatus ordSt, Comparator sortBy,
			GregorianCalendar begin, GregorianCalendar end) {
		WorkerWithOrder.getOrderByPeriod(orderList, sortBy, ordSt, begin, end);
	}

	public void addMaster(IMaster master) {
		WorkerWithMaster.addMaster(masterList, master);
	}

	public void deleteMaster(IMaster master) {
		WorkerWithMaster.deleteMaster(masterList, master);
	}

	public void addGarage(IGarage garage) {
		WorkerWithGarage.addGarage(garageList, garage);
	}

	public void deleteGarage(IGarage garage) {
		WorkerWithGarage.deleteGarage(garageList, garage);
	}

	public void addOrder(IOrder order) {
		WorkerWithOrder.addOrder(orderList, order);
	}

	public void markOrder(OrderStatus ordSt, IOrder order,
			GregorianCalendar nowDate) {
		WorkerWithOrder.markOrder(orderList, garageList, ordSt, order, nowDate);
	}

	public void ShirtTimeOrder(GregorianCalendar newDate, IOrder order) {
		WorkerWithOrder.ShirtTimeOrder(orderList, newDate, order);
	}

	public void saveData() {
		db.saveGarageListToBD(garageList);
		db.saveMasterListToBD(masterList);
		db.saveOrderListToBD(orderList);
	}

}
